#include "soc_func.h"
//---------------------------------------
//- Example for read and write operation. 
//---------------------------------------
//--- set a bit to 1. 
// GPIO->gpio_p00_config |= gpio_p00_config_p00_pu_bit;
//
//--- set a bit to 0. 
// GPIO->gpio_p00_config &= ~gpio_p00_config_p00_pu_bit;
//
//--- set a bus to another value.
// regVal =  GPIO->gpio_p00_config;
// regVal &=  ~(gpio_p00_config_p00_pid_mask << gpio_p00_config_p00_pid_shift);
// GPIO->gpio_p00_config |= (0x6 << gpio_p00_config_p00_pid_shift);

void system_reset()
{
  int regVal=0;
  // read 0xE000ED0C, and then change key to 0x05fa, and write bit [2] = 1. 
  regVal = soc_AIRCR[0x0];
  regVal = (0x0000ffff & regVal) | 0x05fa0004;
  soc_AIRCR[0x0] = regVal;
}

void switch_to_50m()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- switch to DCXO.
  //-----------------------------------------------
  regVal = PMU->pmu_clk_high_reg;
  regVal &= ~pmu_clk_high_reg_ctrl_high_clk_sel_mask << pmu_clk_high_reg_ctrl_high_clk_sel_shift;
  regVal |= 0x0 << pmu_clk_high_reg_ctrl_high_clk_sel_shift;
  PMU->pmu_clk_high_reg = regVal;

  for(i=0;i<10;i=i+1);

}

void switch_to_plli()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- switch to plli.
  //-----------------------------------------------
  regVal = PMU->pmu_clk_high_reg;
  regVal &= ~pmu_clk_high_reg_ctrl_high_clk_sel_mask << pmu_clk_high_reg_ctrl_high_clk_sel_shift;
  regVal |= 0x1 << pmu_clk_high_reg_ctrl_high_clk_sel_shift;
  PMU->pmu_clk_high_reg = regVal;

  for(i=0;i<10;i=i+1);

}

void config_uart0_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p00 as uart0_txd, and p01 as uart0_rxd.
  //-----------------------------------------------
  GPIO->gpio_p00_config = 0x01; //uart0
  GPIO->gpio_p01_config = 0x01; //uart0

}

void config_i2c0_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p07 as i2c0_scl, and p08 as i2c0_sda.
  //-----------------------------------------------
  GPIO->gpio_p07_config = 0x81; // pull-up
  GPIO->gpio_p08_config = 0x81; // pull-up
}

void config_i2c1_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p17 as i2c0_scl, and p18 as i2c0_sda.
  //-----------------------------------------------
  GPIO->gpio_p17_config = 0x81; // pull-up
  GPIO->gpio_p18_config = 0x81; // pull-up
}


void mcu_idle()
{
  soc_SCR[0x0] = 0x00;
  //asm("WFI");
  return;
}

void mcu_sleep()
{
  //--- MUST make sure PMU's state is idle before going to sleep. 
  int regVal=0;
  regVal = PMU->pmu_pmu_state;
  regVal = regVal & (pmu_pmu_state_cur_state_mask << pmu_pmu_state_cur_state_shift);
  while( regVal != 0x00 )
  {
    regVal = PMU->pmu_pmu_state;
    regVal = regVal & (pmu_pmu_state_cur_state_mask << pmu_pmu_state_cur_state_shift);
  }

  soc_SCR[0x0] = 0x04;
  //asm("WFI");
  return;
}

void config_spi0_master_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p03~p06 as spi0 master
  //-----------------------------------------------
  GPIO->gpio_p03_config = 0x01; // mclk 
  GPIO->gpio_p04_config = 0x01; // mcsn
  GPIO->gpio_p05_config = 0x01; // miso
  GPIO->gpio_p06_config = 0x01; // mosi
}

void config_spi0_slave_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p03~p06 as spi0 slave
  //-----------------------------------------------
  GPIO->gpio_p03_config = 0x02; // mclk 
  GPIO->gpio_p04_config = 0x02; // mcsn
  GPIO->gpio_p05_config = 0x02; // miso
  GPIO->gpio_p06_config = 0x02; // mosi
}

void config_spi1_master_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p13~p16 as spi1 master
  //-----------------------------------------------
  GPIO->gpio_p13_config = 0x01; // mclk 
  GPIO->gpio_p14_config = 0x01; // mcsn
  GPIO->gpio_p15_config = 0x01; // miso
  GPIO->gpio_p16_config = 0x01; // mosi
}

void config_spi1_slave_pad()
{
  int regVal=0;
  int i;

  //-----------------------------------------------
  //--- configure p13~p16 as spi1 slave
  //-----------------------------------------------
  GPIO->gpio_p13_config = 0x02; // mclk 
  GPIO->gpio_p14_config = 0x02; // mcsn
  GPIO->gpio_p15_config = 0x02; // miso
  GPIO->gpio_p16_config = 0x02; // mosi
}

//-----------------------------------------------
//--- delay in us. This is only valid in ASIC simulation. 
//-----------------------------------------------
void delay_us_rtl(int delay_time)
{
  for(int i=0; i<delay_time; i=i+1);
  for(int i=0; i<delay_time; i=i+1);
  for(int i=0; i<delay_time; i=i+1);
  for(int i=0; i<delay_time; i=i+1);
  for(int i=0; i<delay_time; i=i+1);
}


void config_p30_gpio_out_high()
{
  int regVal=0;
  int i;
  GPIO->gpio_p30_config &= ~gpio_p30_config_p30_oen_bit; //output
  GPIO->gpio_out_set    =0x40000000;//output 1
}

void config_p29_gpio_out_high()
{
  int regVal=0;
  int i;
  GPIO->gpio_p29_config &= ~gpio_p29_config_p29_oen_bit; //output
  GPIO->gpio_out_set    =0x20000000;//output 1
}


void config_p30_gpio_out_low()
{
  int regVal=0;
  int i;
  GPIO->gpio_p30_config &= ~gpio_p30_config_p30_oen_bit; //output
  GPIO->gpio_out_clear   =0x40000000;//output 1
}

void config_p29_gpio_out_low()
{
  int regVal=0;
  int i;
  GPIO->gpio_p29_config &= ~gpio_p29_config_p29_oen_bit; //output
  GPIO->gpio_out_clear    =0x20000000;//output 0
}

void config_sim_end_with_pass()
{
//p30=1,p29=1
//
  config_p29_gpio_out_high();
  config_p30_gpio_out_high();
}

void config_sim_end_with_fail()
{
//p30=1,p29=0
//
  config_p29_gpio_out_low(); 
  config_p30_gpio_out_high();
  
}


